package cn.tedu.apps_system.base.interceptors;


import cn.tedu.apps_system.base.response.JsonResult;
import cn.tedu.apps_system.base.response.ResultCode;
import cn.tedu.apps_system.pojo.vo.UserVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

@Slf4j
public class LoginCheckInterceptor implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        log.debug("拦截器...");
        HttpSession session = request.getSession();
        UserVO userVO = (UserVO) session.getAttribute("user");
        if (userVO != null){
            return true;
        }

        // 对于API请求返回JSON响应
        response.setContentType("application/json;charset=UTF-8");
        response.getWriter().write(new JsonResult(ResultCode.UNAUTHORIZED).toString());
        return false;
    }
}